<?php

namespace App\Http\Controllers\Api;

use App\Follow;
use App\Http\Requests\StoreFollow;
use App\UserPersonnel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class FollowController extends Controller
{
    //关注
    public function store(StoreFollow $request, Follow $follow)
    {
        $active_id = Auth::id();
        $data = ['active_id' => $active_id];
        if ($request->position_id) {
            if ($follow::where('active_id', $active_id)->where('position_id', $request->position_id)->first()) {
                return response(['message' => '成功', 'status' => 1]);
            }
            $data['position_id'] = $request->position_id;
        }
        if ($request->company_id) {
            if ($follow::where('active_id', $active_id)->where('company_id', $request->company_id)->first()) {
                return response(['message' => '成功', 'status' => 1]);
            }
            $data['company_id'] = $request->company_id;
        }
        if ($request->by_id) {
            if ($follow::where('active_id', $active_id)->where('by_id', $request->by_id)->first()) {
                return response(['message' => '成功', 'status' => 1]);
            }
            $data['by_id'] = $request->by_id;
        }

        if ($follow::create($data)) {
            return response(['message' => '成功', 'status' => 1]);
        }
        return response(['message' => '失败', 'status' => 0]);
    }

    //获取我的关注
    public function myFollow(Request $request)
    {
        $pageSize = $request->page_size ? $request->page_size : 15;
        $identity=UserPersonnel::where('user_id',Auth::id())->value('identity');
        if ($identity == 1) {//我当前是个人，获取我关注的公司和职位
            $data = Follow::where('active_id', Auth::id())->whereNull('by_id')->with(['position.companyInfo','companyInfo'])->paginate($pageSize);
        }
        if ($identity == 2) {//我当前是公司，获取我关注的个人信息
            $data = Follow::where('active_id', Auth::id())->whereNotNull('by_id')->with(['user','userinfo','basics'])->paginate($pageSize);
        }
        return $data;
    }

}
